package com.sxt.pro.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.SurroundDiscount;
import com.sxt.pro.service.SurroundDiscountService;
import com.sxt.pro.vo.SurroundDiscountVo;
import com.sxt.sys.common.AppFileUtils;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;


/**  

* @Description: 周边优惠控制层

* @author xinxiaokang  

* @date 2020年10月20日 下午4:47:57

*/
@RestController
@RequestMapping("/surroundDiscount")
public class SurroundDiscountController {
	
	@Autowired
	private SurroundDiscountService surroundDiscountService;
	
	/**
	 * 查询周边优惠分页列表
	 * @param surrounddiscountVo
	 * @return
	 */
	@RequestMapping("/loadAllSurroundDiscount")
	public DataGridView loadAllSurroundDiscount(SurroundDiscountVo surrounddiscountVo){
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 surrounddiscountVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<SurroundDiscount> page = new Page<>(surrounddiscountVo.getPage(), surrounddiscountVo.getLimit());
		//封装对象查询条件
		QueryWrapper<SurroundDiscount> queryWrapper = new QueryWrapper<SurroundDiscount>();
		//筛选条件：网点编号
		queryWrapper.eq(surrounddiscountVo.getBanknbr()!=null&&surrounddiscountVo.getBanknbr()!=0,"banknbr",surrounddiscountVo.getBanknbr());
//		//筛选条件:产品类型名称
//		queryWrapper.like(StringUtils.isNotBlank(surrounddiscountVo.getProducttypename()), "producttypename", surrounddiscountVo.getProducttypename());
//		//筛选条件：大于开始时间
//		queryWrapper.ge(surrounddiscountVo.getStartTime()!=null, "producttypetime", surrounddiscountVo.getStartTime());
//		//筛选条件：小于结束时间
//		queryWrapper.le(surrounddiscountVo.getEndTime()!=null, "producttypetime", surrounddiscountVo.getEndTime());
//		queryWrapper.orderByDesc("producttypetime");
		//获取分页数据
		this.surroundDiscountService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加周边优惠活动
	 * @param surrounddiscountVo
	 * @return
	 */
	@RequestMapping("addSurroundDiscount")
	public ResultObj addSurroundDiscount(SurroundDiscountVo surrounddiscountVo) {
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		surrounddiscountVo.setBanknbr(banknbr);
		try {
			if(surrounddiscountVo.getPicture()!=null&&surrounddiscountVo.getPicture().endsWith("_temp")) {
				//图片名称
				String newName= AppFileUtils.renameFile(surrounddiscountVo.getPicture());
				surrounddiscountVo.setPicture(newName);
			}
			if(surrounddiscountVo.getPayimage()!=null&&surrounddiscountVo.getPayimage().endsWith("_temp")) {
				//支付图片名称
				String newName= AppFileUtils.renameFile(surrounddiscountVo.getPayimage());
				surrounddiscountVo.setPayimage(newName);
			}
			this.surroundDiscountService.save(surrounddiscountVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改周边优惠活动
	 * @param surrounddiscountVo
	 * @return
	 */
	@RequestMapping("updateSurroundDiscount")
	public ResultObj updateSurroundDiscount(SurroundDiscountVo surrounddiscountVo) {
		try {
			//说明是不默认图片
			if(!(surrounddiscountVo.getPicture()!=null&&surrounddiscountVo.getPicture().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(surrounddiscountVo.getPicture().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(surrounddiscountVo.getPicture());
					surrounddiscountVo.setPicture(newName);
					//删除原先的图片
					String oldPath=this.surroundDiscountService.getById(surrounddiscountVo.getId()).getPicture();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			if(!(surrounddiscountVo.getPayimage()!=null&&surrounddiscountVo.getPayimage().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(surrounddiscountVo.getPayimage().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(surrounddiscountVo.getPayimage());
					surrounddiscountVo.setPayimage(newName);
					//删除原先的图片
					String oldPath=this.surroundDiscountService.getById(surrounddiscountVo.getId()).getPayimage();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			this.surroundDiscountService.updateById(surrounddiscountVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除周边优惠活动
	 * @param id
	 * @param picture
	 * @return
	 */
	@RequestMapping("deleteSurroundDiscount")
	public ResultObj deleteSurroundDiscount(Integer id,String picture,String payimage) {
		try {
			//删除原文件
			if (!picture.isEmpty() && !picture.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(picture);
			}
			if (!payimage.isEmpty() && !payimage.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(payimage);
			}
			this.surroundDiscountService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
}
